package com.zzyl.controller;

import com.zzyl.BaseController.BaseController;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 访问角色管理
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页查询")
    public ResponseResult page(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize ,
                               @RequestBody RoleDto roleDto){
        roleDto.setPageNum(pageNum);
        roleDto.setPageSize(pageSize);
        PageResponse<RoleVo> pageResponse = roleService.page(pageNum,pageSize,roleDto);

        return success(pageResponse);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult findCheckedResources(@PathVariable Long roleId){
    List<String> list =  roleService.findCheckedResources(roleId);
    return success(list);
    }

    @PostMapping("/init-roles")
    @ApiOperation("查询所有角色")
    public ResponseResult selectAll(){
        List<RoleVo> list = roleService.selectAll();
        return success(list);
    }

    @PatchMapping
    @ApiOperation("角色修改（包含禁用）")
    public ResponseResult update(@RequestBody RoleDto roleDto){
        roleService.update(roleDto);
        return success();
    }

    @PutMapping
    @ApiOperation("新增角色")
    public ResponseResult insert(@RequestBody RoleDto roleDto){
        roleService.insert(roleDto);
        return success();
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation("删除角色")
    public ResponseResult deleteById(@PathVariable Long roleIds){
        roleService.deleteById(roleIds);
        return success();
    }
}
